package com.media.publish

import androidx.lifecycle.MutableLiveData
import com.baselibrary.bean.*
import com.baselibrary.net.APIResponse
import com.baselibrary.net.MyHttpUtil
import com.baselibrary.utils.SpUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class CommunityRequestManager {
    companion object {
        @Volatile
        private var instance: CommunityRequestManager? = null

        fun getInstance(): CommunityRequestManager {
            return instance ?: synchronized(this) {
                instance ?: CommunityRequestManager().also { instance = it }
            }
        }
    }


    fun reqSearchTopic(
        tagCategoryId: Long, type: Int, listData: MutableLiveData<List<ReleaseTopicBean.DataBean>>
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqSearchTopic(it, tagCategoryId, Int.MAX_VALUE.toLong(), type)
                .subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<List<ReleaseTopicBean.DataBean>>(this) {
                    override fun onSuccess(t: List<ReleaseTopicBean.DataBean>, json: String) {
                        listData.value = t
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        listData.value = null
                    }
                })
        }
    }

}